package com.xbh.Book.web;

import com.google.gson.Gson;
import com.xbh.Book.pojo.Book;
import com.xbh.Book.pojo.Cart;
import com.xbh.Book.pojo.CartItem;
import com.xbh.Book.service.BookService;
import com.xbh.Book.service.impl.BookServiceImpl;
import com.xbh.Book.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: JavaWebExer
 * @description:
 * @author: 许宝华
 * @create: 2021-05-03 10:19
 */

public class CartServlet extends BaseServlet{
    private  BookService bookService=new BookServiceImpl();
    /**
     * 加入购物车
     * @param req
     * @param resp
     * @return void
     * @author 许宝华
     * @creed: Talk is cheap,show me the code
     * @date 2021/5/3 10:20
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数：商品编号
        int id= WebUtils.parseInt(req.getParameter("id"),0);
        //调用bookService。queryBookId()得到图书的信息
        Book book = bookService.queryBookById(id);
        //把图书信息转换为cartItem商品项
        CartItem cartItem=new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //调用cart.addItem()添加商品项
        Cart cart=(Cart) req.getSession().getAttribute("cart");
        /**
         * 判断购物车是否为空，始终要保持只有一个购物车
         */
        if(cart==null){
            cart=new Cart();
            req.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);
//        System.out.println(cart);
//        System.out.println("请求头referer的值"+req.getHeader("Referer"));
        //最后一个添加的商品
        req.getSession().setAttribute("lastName",cartItem.getName());
        //重定向回原来商品所在的页面,从哪个地址来回到哪个地址中
//        resp.sendRedirect(req.getHeader("Referer"));
        //通过ajax返回
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("totalCount",cart.getTotalCount());
        resultMap.put("lastName",cartItem.getName());
        Gson gson=new Gson();
        String json = gson.toJson(resultMap);
        resp.getWriter().write(json);
    }
    /**
     * 删除购物车里的商品
     * @param req
     * @param resp
     * @return void
     * @author 许宝华
     * @creed: Talk is cheap,show me the code
     * @date 2021/5/3 10:20
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取商品的编号
        int id=WebUtils.parseInt(req.getParameter("id"),0);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            //删除购物车的商品项
            cart.deleteItem(id);
            //重定向回原来的商品展示页面
            resp.sendRedirect(req.getHeader("Referer"));

        }
    }
    /**
     * 清空购物车
     * @param req
     * @param resp
     * @return void
     * @author 许宝华
     * @creed: Talk is cheap,show me the code
     * @date 2021/5/3 10:20
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取购物车对象
        Cart cart=(Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.clear();
            resp.sendRedirect(req.getHeader("Referer"));

        }
    }
    /**
     * 修改购物车商品的数量
     * @param req
     * @param resp
     * @return void
     * @author 许宝华
     * @creed: Talk is cheap,show me the code
     * @date 2021/5/3 10:20
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=WebUtils.parseInt(req.getParameter("id"),0);
        int count=WebUtils.parseInt(req.getParameter("count"),1);
        //获取购物车对象
        Cart cart=(Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.updateCount(id,count);
            resp.sendRedirect(req.getHeader("Referer"));

        }
    }

}
